package com.leapp.sk.ui.activity.im

import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.view.WindowManager
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMChatType
import com.example.baselibrary.R
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.example.util.BCFinalist
import com.leapp.sk.binder.im.SingleChatBinder
import com.example.im.SKIMSend
import com.leapp.sk.inter.ImagePathInterface
import com.example.util.im.MessagePresenter
import com.example.util.finallist.MESSAGE_LIST_ID
import com.example.util.finallist.TECHER_NICK
import com.leapp.sk.util.getNoCompressResultFile
import com.leapp.sk.viewdelegate.im.SingleChatDelegale

/**
 * 单聊页面
 * @author  chaohaipeng  on 2018/7/13.
 */
class SingleChatActivity: BaseActivityMVP<SingleChatDelegale>(), ImagePathInterface {
    /**
     * 消息观察者
     */
    var messagePresenter: MessagePresenter?=null
    /**
     * 会话Id
     */
    var conversationId:String?=null

    override fun initView(p0: Bundle?) {
        this.window.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.bkF8color))
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
         conversationId = intent?.getStringExtra(MESSAGE_LIST_ID)
        val nick = intent?.getStringExtra(TECHER_NICK)
        viewDelegate.initTobBar(nick)
        if (conversationId!=null){
            viewDelegate.initView(conversationId!!)
        }
        viewDelegate.initMegListener(messagePresenter)
    }

    override fun initDate() {
        messagePresenter= MessagePresenter()
        messagePresenter?.start()
    }

    override fun bindEvenListener() {
    }

    override fun getDelegateClass(): Class<SingleChatDelegale> {
        return SingleChatDelegale::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return SingleChatBinder()
    }

    override fun onDestroy() {
        super.onDestroy()
        messagePresenter?.stopMessagePresenter()
    }

    override fun onBackPressed() {
        if (viewDelegate.meunIsOpen()){
        }else{
            finish()
        }

    }
    override fun getArrayImagePath(path: List<String>) {
        if (conversationId!=null){
            for (i in path.indices){
                SKIMSend.getInstance().sendImage(conversationId!!,path.get(i),LKIMChatType.SingleChat)
            }
        }

    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        getNoCompressResultFile(requestCode, resultCode, data, this,this)
        when(requestCode){
            BCFinalist.VIDEO_PLAY_REQUEST->{
                if (resultCode == BCFinalist.VIDEO_PLAY_REQUEST) {
                   val videoPath = data?.getStringExtra(BCFinalist.PATH_VIDEO_PATH)
                    val imagePath = data?.getStringExtra(BCFinalist.PATH_VIDEO_FRIST_IMAGE)
                    val duration = data?.getIntExtra(BCFinalist.PATH_VIDEO_DURATION,0)
                    if (videoPath!=null&&imagePath!=null&&duration!=null&&conversationId!=null) {
                        SKIMSend.getInstance().sendVideo(conversationId!!,videoPath,imagePath,duration,LKIMChatType.SingleChat)
                    }

                }
            }

        }


    }



}